package com.rf.richfitwheel.outbill.controller;


import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.admin.utils.UserUtil;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.outbill.model.OutBillInfo;
import com.rf.richfitwheel.outbill.service.OutBillInfoService;
import com.rf.richfitwheel.utils.OrgVoUtils;
import com.rf.richfitwheel.utils.PageJson;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-06-11
 */
@Api(tags = "出票登记")
@RestController
@RequestMapping("/outbill/outBillInfo")
public class OutBillInfoController {

    @Autowired
    private OutBillInfoService outBillInfoService;

    @Autowired
    private OrgVoUtils orgVoUtils;


    /**
     * 通过ID查询信息
     *
     * @param id
     * @return
     */
    @ApiOperation("出票登记详情")
    @GetMapping("/detail/{id}")
    public OutBillInfo detail(@PathVariable("id") @ApiParam("主键id") String id) {
        QueryWrapper<OutBillInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.select(OutBillInfo.class, v -> !(v.getColumn().equals("update_time") || v.getColumn().equals("create_time")));
        OutBillInfo outBillInfo = outBillInfoService.getOne(queryWrapper);
        return outBillInfo;
    }

    /**
     * 出票登记作废
     */
    @ApiOperation("出票登记作废")
    @GetMapping("/cancel/{id}")
    public Boolean cancel(@PathVariable("id") @ApiParam("主键id") String id) {
        QueryWrapper<OutBillInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        OutBillInfo outBillInfo = outBillInfoService.getOne(queryWrapper);
        outBillInfo.setApvst("6");
        return outBillInfoService.updateById(outBillInfo);
    }

    /**
     * 分页查询信息
     *
     * @param
     * @return
     */
    @ApiOperation("分页查询")
    @PostMapping("/page")
    public PageJson Page(@RequestBody OutBillInfo outBillInfo) {
        return outBillInfoService.pageJson(outBillInfo);
    }

    /**
     * 添加
     *
     * @param outBillInfo
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/add")
    public boolean save(@RequestBody OutBillInfo outBillInfo) {
        //1.人工登記
        outBillInfo.setDataSource("1");
        outBillInfo.setApvst("2");
        UserVO user = UserUtil.getUser();
        outBillInfo.setCreateName(user.getStaffName());
        outBillInfo.setUpdateName(user.getStaffName());
        outBillInfo.setUnitName(orgVoUtils.getParentOrgAndGrandpa(user).getParentOrg().getOrgName());
        outBillInfo.setUnitNumber(orgVoUtils.getParentOrgAndGrandpa(user).getParentOrg().getOrgCode());
        if (StringUtils.isNotBlank(outBillInfo.getId())) {
            return outBillInfoService.updateById(outBillInfo);
        } else {
            return outBillInfoService.save(outBillInfo);
        }

    }

    /**
     * 出票登记添加走流程
     *
     * @param outBillInfo
     * @return
     */
    @PostMapping("/addFlow")
    public boolean addFlow(@RequestBody Map<String, Object> params) {
        return outBillInfoService.addFlow(params);
    }

    /**
     * 出票登记更新走流程
     *
     * @param modifyFlow
     * @return
     */
    @PostMapping("/modifyFlow")
    @ApiOperation("提示承兌更新方法，审批人走流程使用")
    public boolean modifyFlow(@RequestBody Map<String, Object> params) {
        return outBillInfoService.modifyFlow(params, UserUtil.getUser());
    }

    /**
     * 修改
     *
     * @param outBillInfo
     * @return
     */
    @PostMapping("/modify")
    public boolean modify(@RequestBody OutBillInfo outBillInfo) {
        return outBillInfoService.updateById(outBillInfo);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public boolean delBatch(@RequestParam("ids") List<String> ids) {
        return outBillInfoService.removeByIds(ids);
    }

    /**
     * 通过ID删除信息
     *
     * @param id
     * @return
     */
    @GetMapping("/delById/{id}")
    public boolean delById(@PathVariable("id") String id) {
        return outBillInfoService.removeById(id);
    }

    /**
     * 撤销出票登记走流程
     *
     * @param outBillInfo
     * @return
     */
    @PostMapping("/backFlow")
    @ApiOperation("撤回出票登记提交接口")
    public boolean backFlow(@RequestBody @ApiParam("param传outBillInfo和wfTaskParam对象") Map<String, Object> params) {
        return outBillInfoService.backFlow(params);
    }

    /**
     * 撤销出票登记更新走流程
     *
     * @param modifyFlow
     * @return
     */
    @PostMapping("/backModifyFlow")
    @ApiOperation("撤销出票登记更新方法，审批人走流程使用")
    public boolean backModifyFlow(@RequestBody @ApiParam("param传outBillInfo和wfTaskParam对象") Map<String, Object> params) {
        return outBillInfoService.backModifyFlow(params,UserUtil.getUser());
    }
}
